package com.itheima.controller;


import com.itheima.Utils.AliyunOSSUtils;
import com.itheima.pojo.AliyunOSSProperties;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        // 上传oss
        log.info("文件上传: {}", file.getOriginalFilename());
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        log.info("文件上传: {}", extName);
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), file.getBytes(), extName);
        log.info("文件上传: {}", url);
        return Result.success(url);
    }
    //    @PostMapping("/upload")
    //    public Result upload(MultipartFile file) throws IOException {
    //        //获取原始文件名
    //        String originalFilename = file.getOriginalFilename();
    //        //构建新的文件名
    //        String newFileName = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
    //        //将文件保存在服务器端 D:/images/ 目录下
    //        file.transferTo(new File("D:/images/"+newFileName));
    //        return Result.success();
    //    }

}
